import myAxios from "@/util/request";


/**
 * 用户登录
 */
interface LoginParams {
    phone: string;
    verifyCode: number;
}
export const userLogin = async (params: LoginParams) => {
    return myAxios.request({
        url: "/user/login",
        method: "POST",
        data: params,
    })
}


/**
 * 获取当前用户
 */
export const getCurrentUser = async () => {
    return myAxios.request({
        url: "/user/current",
        method: "GET",
        headers: {
            "Authorization": localStorage.getItem("token"),
        }
    })
}

/**
 * 获取当前管理用户
 */
export const getCurrentAdminUser = async () => {
    const adminToken = localStorage.getItem("adminToken");
    console.log('adminToken:', adminToken);
    return myAxios.request({
        url: "/user/current",
        method: "GET",
        headers: {
            "Authorization": localStorage.getItem("adminToken"),
        }
    })
}


/**
 * 编辑用户信息
 */
interface EditUserParams {
    userName: string;
    avatar?: string;
}

export const editUser = async (params: EditUserParams) => {
    return myAxios.request({
        url: "/user/edit",
        method: "POST",
        data: params,
        headers: {
            "Authorization": localStorage.getItem("token"),
        }
    });
}

interface BindRoleParams {
    storeId: string;
    userId: string;
    roleId: number;
}

/**
 * 添加员工
 */
export const bindRole = async (params: BindRoleParams) => {
    return myAxios.request({
        url: "/user/bindRole",
        method: "POST",
        data: params,
        headers: {
            "Authorization": localStorage.getItem("token"),
        }
    });
}

export const getStoreUser = async (storeId: string, pageNum:number, pageSize:number) => {
    return myAxios.request({
        url: "/user/listStoreUser",
        method: "get",
        headers: {
            "Authorization": localStorage.getItem("token"),
        },
        params: {
            storeId: storeId,
            pageNum: pageNum,
            pageSize: pageSize
        }
    })
}

/**
 * 搜索用户
 */
export const search = async (phone: string) => {
    return myAxios.request({
        url: "/user/search",
        method: "get",
        headers: {
            "Authorization": localStorage.getItem("token"),
        },
        params: {
            phone: phone
        }
    })
}

/**
 * 删除员工身份
 */
export const delUserRole = async (storeId: string, userId: string) => {
    return myAxios.request({
        url: "/user/delUserRole",
        method: "get",
        headers: {
            "Authorization": localStorage.getItem("token"),
        },
        params: {
            storeId: storeId,
            userId: userId
        }
    })
}




